# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: 04_只初始化一次.py
# Time: 22:14


class Dog(object):
    __instance = None
    __init_flag = False

    def __new__(cls, *args, **kwargs):
        if cls.__instance == None:
            cls.__instance = object.__new__(cls)
            return cls.__instance
        else:
            # return 上一次创建的对象的引用
            return cls.__instance

    def __init__(self, name):
        if Dog.__init_flag == False:
            # 该对象，需要只初始化一次，在类属性中如果初始化一次则把__init_flag更改为True，
            # 后面不管你初始化几次，__init_flag已经是True不会再初始化name属性
            self.name = name
            Dog.__init_flag = True


a = Dog("旺财")
print(id(a))
print(a.name)

b = Dog("哮天犬")
print(id(b))
print(b.name)
